function getCookie(name: string): string | null {
  let arr: RegExpMatchArray | null;
  const req: RegExp = new RegExp('(^| )' + name + '=([^;]*)(;|$)');
  arr = document.cookie.match(req);
  if (arr) {
    return unescape(arr[2]);
  } else {
    return null;
  }
}

function setCookie(key: string, value: string | null, day: number = 0): void {
  const date: Date = new Date();
  date.setTime(date.getTime() + day * 24 * 3600 * 1000);
  document.cookie = `${key}=${value};expires=${date.toUTCString()};path=/`;
}

function delCookie(key: string): void {
  const date: Date = new Date();
  date.setTime(date.getTime() - 1);
  const delValue: boolean = getCookie(key) !== null;
  if (delValue) {
    document.cookie = `${key}=${delValue};expires=${date.toUTCString()};path=/`;
  }
}

class CookieUtilType {
  public get!: (name: string) => string | null;
  public set!: (key: string, value: string | null, day: number) => void;
  public del!: (key: string) => void;
}
export const cookieUtil: CookieUtilType = {
  get: getCookie,
  set: setCookie,
  del: delCookie,
};
